import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_common_app/utils/common_util/shared_preferences/sp_util.dart';
import 'package:flutter_common_app/widgets/audio_bottom/audio_bottom_widget.dart';
import 'package:flutter_common_app/env/env_banner.dart';
import 'package:flutter_common_app/pages/splash_page.dart';
import 'package:flutter_common_app/provider_model/user_model.dart';
import 'package:flutter_common_app/router/routes.dart';
import 'package:flutter_common_app/provider_model/audio_model.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();

class MyApp extends StatelessWidget {
  MyApp() {
    final FluroRouter fluroRouter = FluroRouter();
    Routes.configureRoutes(fluroRouter);
    Routes.router = fluroRouter;

    SpUtil.getInstance();
  }

  @override
  Widget build(BuildContext context) {
    return EvnBanner(
      child: ScreenUtilInit(
        designSize: Size(750, 1334),
        builder: () => MultiProvider(
          providers: [
            ChangeNotifierProvider(create: (_) => UserModel()),
            ChangeNotifierProvider(create: (_) => AudioModel()..init()),
          ],
          child: MaterialApp(
            debugShowCheckedModeBanner: false,
            title: 'NetMusic',
            theme: ThemeData(
              primaryColor: Colors.white,
              visualDensity: VisualDensity.adaptivePlatformDensity,
            ),
            home: LayoutBuilder(
              builder: (context, constraints) {
                WidgetsBinding.instance
                    .addPostFrameCallback((_) => _insertOverlay(context));
                return SplashPage();
              },
            ),
            onGenerateRoute: Routes.router.generator,
            navigatorKey: navigatorKey,
          ),
        ),
      ),
    );
  }

  void _insertOverlay(BuildContext _context) {
    OverlayEntry entry = OverlayEntry(builder: (_context) {
      return AudioBottomWidget();
    });
    return Overlay.of(_context).insert(entry);
  }
}
